package com.meadvc.ssmdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.meadvc.ssmdemo.service.ComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: ssm-demo
 * @description: 零件控制类
 * @author: meadvc
 * @create: 2020-06-15 21:50
 **/
@RestController
@RequestMapping("/comp")
public class ComponentController {
    @Autowired
    private ComponentService comps;
    /**
     * 查询零件列表
     */
    @GetMapping("/listComp")
    @CrossOrigin
    public JSONObject listComponent(HttpServletRequest request) {
        String query = request.getParameter("query");
        int pagenum = Integer.parseInt(request.getParameter("pagenum"));
        int pagesize = Integer.parseInt(request.getParameter("pagesize"));
        return comps.listComps(query,pagenum,pagesize);
    }

    /**
     * 根据id得到零件
     * @param request
     * @return
     */
    @GetMapping("/getComp")
    @CrossOrigin
    public JSONObject getComponent(HttpServletRequest request) {
        //int cid =requestJson.getIntValue("cid");
        int cid = Integer.parseInt(request.getParameter("cid"));
        return comps.getComps(cid);
    }
    @GetMapping("/getCompByName")
    @CrossOrigin
    public JSONObject getComponentByName(HttpServletRequest request) {

        String cname = request.getParameter("cname");
        return comps.getCompsByName(cname);
    }


    @PostMapping("addComp")
    @CrossOrigin
    public JSONObject addComponent(@RequestBody JSONObject requestJson) {
        return comps.addComps(requestJson);
    }
    @PostMapping("updateComp")
    @CrossOrigin
    public JSONObject updateComponent(@RequestBody JSONObject requestJson) {
        return comps.updateComps(requestJson);
    }
    @PostMapping("deleteComp")
    @CrossOrigin
    public JSONObject deleteComponent(@RequestBody JSONObject requestJson) {
        return comps.deleteComps(requestJson);
    }
    //得到供应商的信息
    @GetMapping("listSuppliers")
    @CrossOrigin
    public JSONObject listSuppliers(){
        return  comps.listSuppliers();
    }
    //得到所有的零件名称
    @GetMapping("listCompNames")
    @CrossOrigin
    public JSONObject listCompNames(){
        return  comps.listCompNames();
    }




}
